package com.medusa.aps.business.model.enums;

import cn.hutool.core.text.StrPool;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.annotation.EnumValue;
import lombok.Getter;
import lombok.RequiredArgsConstructor;

import java.util.regex.Pattern;

/**
 * 菜单类型
 *
 * @author jinbu
 */
@Getter
@RequiredArgsConstructor
public enum MenuType{

    /**
     * 菜单
     */
    MENU(1, "菜单", false),

    /**
     * 按钮
     */
    BUTTON(2, "按钮", true);

    private static final Pattern FIRST_SLASH_MATCH = Pattern.compile("^/+");
    @EnumValue
    private final Integer value;
    private final String desc;
    private final boolean permission;

    /**
     * path 转 权限
     *
     * @param path route path/uri 路径
     * @return 转后权限
     */
    public String convertPerm(String path) {
        if (StrUtil.isBlank(path)) {
            return null;
        }
        if (this == MENU) {
            return path;
        }
        return FIRST_SLASH_MATCH.matcher(path).replaceAll(StrUtil.EMPTY).replace(StrPool.SLASH, StrPool.COLON);
    }

}
